package org.graduation.recruitment.controller.resume;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.graduation.recruitment.entity.pojo.resume.InternshipExperience;
import org.graduation.recruitment.service.resume.InternshipExperienceService;
import org.graduation.recruitment.utils.ResponseResult;
import org.graduation.recruitment.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resume/internship_experience")
@Slf4j
@Tag(name = "简历中实习经验接口", description = "提供简历中实习经验相关的CRUD操作")
@SecurityRequirement(name = "openApiSecurityScheme")
public class InternshipExperienceController {

    // 注入实习经验服务层
    @Autowired
    private InternshipExperienceService internshipExperienceService;
    // 注入Token工具类
    @Autowired
    private Token token;

    /**
     * 创建实习经验
     *
     * @param tokenStr             用户令牌
     * @param internshipExperience 实习经验实体
     * @return 创建成功返回成功信息，否则返回失败信息
     */
    @PostMapping("/create")
    @Operation(summary = "创建实习经验接口")
    public ResponseResult createInternshipExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody InternshipExperience internshipExperience) {
        // 设置学生ID
        internshipExperience.setStudentId(token.getUidByToken(tokenStr));
        // 保存实习经验
        if (internshipExperienceService.save(internshipExperience)) {
            return ResponseResult.success(internshipExperience);
        } else {
            return ResponseResult.fail("创建实习经验失败");
        }
    }

    /**
     * 更新实习经验
     *
     * @param tokenStr             用户令牌
     * @param internshipExperience 实习经验实体
     * @return 更新成功返回成功信息，否则返回失败信息
     */
    @PostMapping("/update")
    @Operation(summary = "更新实习经验接口")
    public ResponseResult updateInternshipExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody InternshipExperience internshipExperience) {
        // 验证是否为本人操作
        if (!token.getUidByToken(tokenStr).equals(internshipExperience.getStudentId())) {
            return ResponseResult.fail("只能修改自己的实习经验");
        }
        // 更新实习经验
        if (internshipExperienceService.updateById(internshipExperience)) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail("更新实习经验失败");
        }
    }

    /**
     * 删除实习经验
     *
     * @param internshipExperience 实习经验实体
     * @return 删除成功返回成功信息，否则返回失败信息
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除实习经验接口")
    public ResponseResult deleteInternshipExperience(@RequestBody InternshipExperience internshipExperience) {
        // 删除实习经验
        if (internshipExperienceService.removeById(internshipExperience.getId())) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail("删除实习经验失败");
        }
    }

    /**
     * 根据简历ID获取所有实习经验
     *
     * @param resumeId 简历ID
     * @return 返回对应简历ID的所有实习经验
     */
    @GetMapping("/all")
    @Operation(summary = "查看对应resume id的所有实习经验接口")
    public ResponseResult getAllInternshipExperience(@RequestParam Long resumeId) {
        // 获取所有实习经验
        return ResponseResult.success(internshipExperienceService.getAllByResumeId(resumeId));
    }
}
